/**
 * Created by Ren on 2015/5/21.
 */
$(function(){
    $(document).keydown(function(e){
        if(!keyStatus.KEY_ANY_Status)
        {
            if(gameStatus.game_Status == 2)
            {
                if(gameStatus.game_isPlay)
                {
                    setTimeout(function()
                    {
                        keyStatus.KEY_ANY_Status = true;
                    },200);
                }
            }
            else
            {
                keyStatus.KEY_ANY_Status = true;
            }
        }
        else
        {
            switch (e.which)
            {
                case keyCode.KEY_LEFT:
                    keyStatus.KEY_LEFT_Status=true;
                    break;
                case keyCode.KEY_RIGHT:
                    keyStatus.KEY_RIGHT_Status=true;
                    break;
                case keyCode.KEY_DOWN:
                    keyStatus.KEY_DOWN_Status=true;
                    break;
                case keyCode.KEY_UP:
                    keyStatus.KEY_UP_Status=true;
                    break;
                case keyCode.KEY_ESC:
                    keyStatus.KEY_ESC_Status=true;
                    break;
                case keyCode.KEY_SPACE:
                    keyStatus.KEY_SPACE_Status=true;
                    break;
            }
            if(keyStatus.KEY_ESC_Status)
            {
                if(director.status == 1)
                {
                    director.pause();
                }
                else
                {
                    director.play();
                }
            }
        }
    }).keyup(function(e){
        switch (e.which)
        {
            case keyCode.KEY_LEFT:
                keyStatus.KEY_LEFT_Status=false;
                break;
            case keyCode.KEY_RIGHT:
                keyStatus.KEY_RIGHT_Status=false;
                break;
            case keyCode.KEY_DOWN:
                keyStatus.KEY_DOWN_Status=false;
                break;
            case keyCode.KEY_UP:
                keyStatus.KEY_UP_Status=false;
                break;
            case keyCode.KEY_ESC:
                keyStatus.KEY_ESC_Status=false;
                break;
            case keyCode.KEY_SPACE:
                keyStatus.KEY_SPACE_Status=false;
                break;
        }
    })
    var director = new Director();
    director.ctx = $("#game_canvas")[0].getContext("2d");
    director.backAudio = $("#backAudio")[0];
    director.fireAudio = $("#fireAudio")[0];
    director.bulletExplodeAudio = $("#bulletAudio")[0];
    director.playerExplodeAudio = $("#playerAudio")[0];
    director.enimyExplodeAudio = $("#enemyAudio")[0];
    director.back = new BackGround(director.ctx, director.starArray);
    director.player = new Player(director.ctx, director.fireAudio, director.playerBullets, director.playerExplodeAudio, director.bulletExplodeAudio);
    director.play();
});